<?php
namespace Biz\module\Behaviors\Stock;


use Biz\model\GoodsStockLog;
use Biz\service\activity\PinTuan;
use Biz\model\activity\PinTuanSku as mPinTuanSku;
use Biz\cache\PintuanStock as cPintuanStock;

class PintuanStock extends StockBehaviorsAbstruct
{

    const TYPE_LOG = GoodsStockLog::TYPE_LIMIT_PINTUAN;


    public static function setStocksCache($goods_ids)
    {
        $pintuanList = PinTuan::getCurrentList($goods_ids);
        foreach ($pintuanList as $goodsId => $item) {
            $stockInfos = [];
            foreach ($item['skus'] as $pintuanSku) {
               $stockInfos[$pintuanSku['sku_id']] = $pintuanSku['actual_stock'];
            }
            cPintuanStock::setPintuanStock($item['id'], $item['goods_id'], $stockInfos);
        }
    }

    public static function DecrStocksCounter($buyNums)
    {
        return cPintuanStock::DecrStocksCounter($buyNums);
    }


    public static function IncrStocksCounter($buyNums)
    {
        return cPintuanStock::IncrStockCounter($buyNums);
    }

    public static function updateDbStock($current_stock)
    {
        foreach ($current_stock as $skuId => $stock) {
            mPinTuanSku::update([
                'actual_stock' => $stock
            ], [
                'sku_id' => $skuId,
                'start_time[<]' => date('Y-m-d H:i:s'),
                'end_time[>]' => date('Y-m-d H:i:s')
            ]);
        }

    }

}